package database;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * 实现自己的数据库连接池
 * @author 12609
 *
 */
public class MyDBPool implements DataSource{
	
	private static LinkedList<Connection> connectionList = new LinkedList<>();
	
	static {
		//加载本目录下的db.properties数据库配置文件
		//这边properties文件路径是src根目录下
		InputStream inputStream = MyDBPool.class.getClassLoader().getResourceAsStream("db.properties");
		
		
		//访问指定目录的方法
		/*BufferedReader inputStream = null;
		try {
			inputStream = new BufferedReader(new FileReader("C:/Users/12609/eclipse-workspace/forInterview/src/db.properties"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		Properties prop = new Properties();
		
		try {
			prop.load(inputStream);
			//String driver = prop.getProperty("driver");
			String url = prop.getProperty("url");
			String username = prop.getProperty("username");
			String password = prop.getProperty("password");
			Integer poolInitSize = Integer.parseInt(prop.getProperty("poolInitSize"));
			//加载数据库驱动,现在的java 不需要了
			//Class.forName(driver);
			for(int i = 0; i < poolInitSize; i++) {
				Connection connection = DriverManager.getConnection(url,username,password);
				connectionList.add(connection);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//资源关闭
			try {
				if(inputStream != null)
					inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {

		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Connection getConnection() throws SQLException {

		if(connectionList.size() > 0) {
			final Connection conn = connectionList.removeFirst();
			System.out.println("当前连接的类名："+conn.getClass().getName());
			System.out.println("当前数据库连接池大小："+connectionList.size());
			return (Connection) Proxy.newProxyInstance(MyDBPool.class.getClassLoader(), conn.getClass().getInterfaces(), 
					new InvocationHandler() {
				
				@Override
				public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

					if(!method.getName().equals("close")) {
						System.err.println("断开一次连接");
						return method.invoke(conn, args);
					}else {
						connectionList.add(conn);
						System.out.println("当前连接重新进入连接池！！！");
						System.out.println("当前数据库连接池大小："+connectionList.size());
						return null;
					}
					
				}
			});
		}else {
			throw new RuntimeException("数据库正忙，请稍等再试！");
		}
		
		
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
